package com.wqnmdb.im.controller;

import com.alibaba.fastjson.JSON;
import com.wqnmdb.im.domain.contants.ConstantEnum;
import com.wqnmdb.im.domain.req.ReqRegisterUserModel;
import com.wqnmdb.im.domain.req.ReqRegisterAppModel;
import com.wqnmdb.im.domain.resp.R;
import com.wqnmdb.im.exception.ParamsException;
import com.wqnmdb.im.exception.ServiceException;
import com.wqnmdb.im.service.RegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/register")
@Api(value = "/register", tags = {"注册服务"})
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @PostMapping("/app")
    @ApiOperation(value = "注册APP", notes = "令牌、APP名称必填")
    public ResponseEntity<R> app(@RequestBody @Valid ReqRegisterAppModel model, BindingResult bindingResult) {
        String methodName = "注册APP";
        log.info(methodName + "-请求报文: {}.", JSON.toJSONString(model));
        //参数验证~
        if (bindingResult.hasErrors()) {
            throw new ParamsException(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        try {
            ResponseEntity<R> result = registerService.app(model.getAuth(), model.getAppName());
            //返回结果~
            log.info(methodName + "-响应报文: {}.", JSON.toJSONString(result));
            return result;
        } catch (Exception ex) {
            log.error(methodName + "-异常：" + ex.getMessage(), ex);
            throw new ServiceException(ConstantEnum.SERVER_ERR);
        }
    }

    @PostMapping("/user")
    @ApiOperation(value = "注册用户", notes = "令牌、APP名称必填")
    public ResponseEntity<R> user(@RequestBody @Valid ReqRegisterUserModel model, BindingResult bindingResult) {
        String methodName = "注册用户";
        log.info(methodName + "-请求报文: {}.", JSON.toJSONString(model));
        //参数验证~
        if (bindingResult.hasErrors()) {
            throw new ParamsException(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        try {
            ResponseEntity<R> result = registerService.user(model.getAccessKey(), model.getSecretKey(), model.getUserId(), model.getOs());
            //返回结果~
            log.info(methodName + "-响应报文: {}.", JSON.toJSONString(result));
            return result;
        } catch (Exception ex) {
            log.error(methodName + "-异常：" + ex.getMessage(), ex);
            throw new ServiceException(ConstantEnum.SERVER_ERR);
        }
    }

    @PostMapping("/getUsers")
    @ApiOperation(value = "获取所有用户", notes = "获取所有用户")
    public ResponseEntity<R> getUsers() {
        String methodName = "获取所有用户";
        try {
            ResponseEntity<R> result = registerService.getUsers();
            //返回结果~
            log.info(methodName + "-响应报文: {}.", JSON.toJSONString(result));
            return result;
        } catch (Exception ex) {
            log.error(methodName + "-异常：" + ex.getMessage(), ex);
            throw new ServiceException(ConstantEnum.SERVER_ERR);
        }
    }
}
